package com.aijk.ehm.sys.api;

import com.aijk.ehm.sys.model.qo.SysAdminQo;
import com.aijk.ehm.sys.service.SysAdminService;
import com.aijk.ehm.sys.util.PasswordHelper;
import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.exception.ErrorCodeConst;
import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.sys.model.SysAdmin;
import com.aijk.ehm.sys.BusinessError;
import com.aijk.ehm.sys.model.SysAdminRole;
import com.aijk.ehm.sys.model.qo.AdminRoleQo;
import com.aijk.ehm.sys.service.SysAdminRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p> 系统管理员接口实现 </p>
 * User: chendw
 * Date: 2016/11/15 17:13
 */
@Service("sysAdminApi")
@Transactional
public class SysAdminApiImpl implements SysAdminApi {

    @Autowired
    private SysAdminService service;

    @Autowired
    private SysAdminRoleService adminRoleService;


    @Override
    public PageInfo<SysAdmin> getPage(SysAdminQo qo) throws ErrorCodeException {
        PagingRequest pagingRequest = new PagingRequest(qo.getPageNo().intValue(), qo.getPageSize().intValue());
        return service.pageList(pagingRequest,qo);
    }

    @Override
    public SysAdmin get(Integer adminId) throws ErrorCodeException {
        return service.findOne(adminId);
    }

    @Override
    public List<SysAdmin> get(SysAdminQo qo) throws ErrorCodeException {
        return service.findAll(qo);
    }

    @Override
    public SysAdmin save(SysAdmin record) throws ErrorCodeException {
        service.save(record);
        return record;
    }

    @Override
    public SysAdmin save(SysAdmin record, String roles) throws ErrorCodeException {
        service.save(record);
        adminRoleService.saveAdminRole(record.getAdminId(),roles);
        return record;
    }

    @Override
    public SysAdmin login(SysAdmin record) throws ErrorCodeException {
        if (Utils.isEmpty(record.getUsername()) ||Utils.isEmpty(record.getPassword()))
            throw new  ErrorCodeException(ErrorCodeConst.GLOBAL_REQCHK_ERROR,"账号密码不能为空!");

        SysAdminQo qo=new SysAdminQo();
        qo.setUsername(record.getUsername());
        List<SysAdmin> admins = service.findAll(qo, 1, 1);
        if(admins.size()<=0)  throw new ErrorCodeException(ErrorCodeConst.GLOBAL_REQCHK_ERROR,String.format("[%s]记录为空", record.getUsername()));
        SysAdmin sysAdmin = admins.get(0);
        if(sysAdmin.getAdminState()!=0)   throw new ErrorCodeException(ErrorCodeConst.GLOBAL_REQCHK_ERROR,"账号被禁用");
        if(!sysAdmin.getPassword().equals(PasswordHelper.encryptPassword(record.getPassword(), sysAdmin.getSalt()))){
            throw new ErrorCodeException(BusinessError.SysAdmin.PASSWORD_NOT_RIGHT,"账号或密码错误");
        }
        sysAdmin.setPassword(null);
        sysAdmin.setSalt(null);
        return sysAdmin;
    }

    @Override
    public int update(SysAdmin record) throws ErrorCodeException {
        return service.update(record);
    }

    @Override
    public int update(SysAdmin record, String roles) throws ErrorCodeException {
        Boolean isSave=false;
        AdminRoleQo qo = new AdminRoleQo();
        qo.setAdminId(record.getAdminId());
        List<SysAdminRole> adminRoles = adminRoleService.find(qo);
        List<Integer> roleList = new ArrayList<>();
        for (SysAdminRole adminRole:adminRoles){
            roleList.add(adminRole.getRoleId());
        }

        List<Integer> rList = new ArrayList<>();
        String[] roleIds = roles.split(",");
        for (String rid:roleIds){
            rList.add(Integer.parseInt(rid));
        }
        if(!roleList.containsAll(rList) ||! rList.containsAll(roleList)){
            adminRoleService.delAdminRole(record.getAdminId());
            isSave =true;
        }
        if(isSave)
        adminRoleService.saveAdminRole(record.getAdminId(),roles);

        return service.update(record);
    }

    @Override
    public int delete(Integer id) throws ErrorCodeException {
        return service.delete(id);
    }

    @Override
    public SysAdmin findByUsername(String username) throws ErrorCodeException {
        return service.findByUsername(username);
    }

    @Override
    public void resetPass(SysAdminQo adminQo) throws ErrorCodeException {
        List<SysAdmin> sysAdmins = service.findAll(adminQo);
        if (sysAdmins != null && sysAdmins.size() > 0) {
            SysAdmin sysAdmin = sysAdmins.get(0);
            sysAdmin.setPassword(PasswordHelper.encryptPassword("123456", sysAdmin.getSalt()));
            service.update(sysAdmin);
        }
    }
}
